{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://raw.githubusercontent.com/Qiskit/qiskit-tutorials/master/images/qiskit-heading.png\" alt=\"Note: In order for images to show up in this jupyter notebook you need to select File => Trusted Notebook\" width=\"500 px\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Quantum Cryptography\n",
    "In this notebook we are going to introduce the theory of **Quantum Cryptography** and one of its possible applications.\n",
    "\n",
    "If you are new to this topic, I suggest you to read the [Introduction notebook](https://github.com/rugantio/Quantum_crypto/blob/master/Introduction.ipynb), where you can find basic required notions of cryptography and quantum computers. If you feel confident about your background, or you just want to take a peek, you can follow up.\n",
    "\n",
    "This notebook consists in a possible implementation of the **BB84** cryptographic protocol on a quantum computer, reproducing **Quantum Key Distribution** and eavesdropper detection. It makes use of IBM's QISKit, a python library that can manipulate quantum circuits, either via a simulation or a real execution on IBM's backend."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Contributors\n",
    "Costantino Carugno\n",
    "\n",
    "### Qiskit Package Versions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'qiskit': '0.10.3',\n",
       " 'qiskit-terra': '0.8.1',\n",
       " 'qiskit-ignis': '0.1.1',\n",
       " 'qiskit-aer': '0.2.1',\n",
       " 'qiskit-ibmq-provider': '0.2.2',\n",
       " 'qiskit-aqua': '0.5.1'}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import qiskit\n",
    "qiskit.__qiskit_version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Requirements\n",
    "Throughout the notebook we will make use of:\n",
    "- QISKit 0.6 (with IBMQ access for remote backend) \n",
    "- Standard python scientific libs stack: numpy etc.\n",
    "- Knowledge of basic Quantum Mechanics is assumed\n",
    "- Notions of traditional Cryptography are helpful although not strictly necessary"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quantum Key Distribution\n",
    "In 1984, building on the work of *Wiesner*, *Charles Bennett*, an IBM's researcher, and *Gilles Brassard*, of the Université de Montréal, developed the first **quantum cryptographic protocol**, which goes under the codename of **BB84**. \n",
    "\n",
    "Suppose that *Alice* and *Bob* are connected via a **quantum channel** that they can use to exchange qubits. This channel is not used directly to send a private message, but only to exchange random qubits that after processing will compose the encryption key.  \n",
    "\n",
    "If key sharing is completed successfully, this key can be used in the well known way as a **one-time pad** (OTP) to produce a safely encrypted message to be delivered over a **classical channel** using symmetrical cryptography. The key should be completely random, as long as the message, and discarded after use; the procedure can be repeated for every message that needs to be delivered. \n",
    "\n",
    "More specifically *Alice* produces an **initial key**, selecting a sequence of **random bits**, '$0$' and '$1$', and picking a sequence of **polarization eigenstates**, with respect to a randomly chosen basis between: **rectilinear** $\\{\\lvert 0 \\rangle,\\  \\lvert 1 \\rangle\\}$ and **diagonal** $\\{\\lvert \\nearrow \\rangle,\\  \\lvert \\searrow \\rangle\\}$.\n",
    "\n",
    "*Alice* encodes the classical bits of the key one by one in a **qubit**, by preparing each qubit in an eigenstate of the basis chosen, so that only by measuring the qubits in the **right basis** one can retrieve with **certainty** the right classical bit, just as it happens with quantum money. In the meantime *Alice* keeps a note (in a **table**) of the basis that she has picked for every single qubit she has encoded.\n",
    "\n",
    "Now, using the quantum channel, she sends the stream of qubits to *Bob*, who is **unaware** of the basis used by *Alice* for the encoding. *Bob* receives these qubits prepared in a certain polarization eigenstate but, due to the **no-cloning theorem**, he is unable to recognize which basis *Alice* used, because he cannot distinguish **non-orthogonal states** with a single measurement. Nonetheless he proceeds anyway with measuring each photon's polarization using a basis chosen randomly (between rectilinear and diagonal), and he keeps a note of the measurement result and the associated basis that he used in a report **table**. \n",
    "\n",
    "Statistically, *Bob* will pick the **right basis**, the same that *Alice* picked, about **$1/2$** of the times, and the wrong basis about **$1/2$** of the times. When he measures using the right basis he correctly retrieves the information bit of the key, but when he picks the wrong basis the information bit is not certain, since with respect to this basis, the qubit is in a **superposition** of the eigenstates of the right bases, and it can collapse in either two of them with equal probability of **$1/2.$**\n",
    "\n",
    "For this reason *Alice* and *Bob* decide to **sift** their key, which in practical terms means that they discard from the key all the bits obtained via measurements made in the wrong basis, since they are not reliable. The price for this action is that the key will lose about **$1/2$** of its length, but the payoff is that they don't need to unveil their measurements, they just need to compare their tables, where they recorded the basis chosen, and they do that **after** the measurement has occurred.\n",
    "\n",
    "So they open the **classical channel** and only now *Alice* tells (publicly) *Bob* which basis she used to encode the key; they **compare** the **tables** and discard the bits obtained measuring qubits in different basis. What they obtain is a perfectly correlate **sifted key**, the same for both of them, ready for use. This key can be employed as a one-time pad and once is used up completely, the procedure can be repeated again to produce a new random key. \n",
    "\n",
    "What happens if we now introduce an **eavesdropper** in the communication? Suppose that *Eve* is able to intercept the qubits that Alice sends to Bob, and that she can also tap the classical communication channel. When she gets hold of the qubits she still doesn't know which basis *Alice* used, just like *Bob*. She is forced to make a guess, and she will pick the wrong basis **$1/2$** of the times. If she measures in the wrong basis she has **$1/2$** probability to make the qubit collapse in the wrong eigenstate, so that on the whole she will have altered about **$1/4$** of the original qubits. This is the main difference with classical crypto: thanks to quantum mechanics observing implies measuring, and if this is not done accordingly, it changes the actual state (key).\n",
    "\n",
    "*Eve* produces a **candidate key** and passes on these (now altered) qubits to Bob who proceeds himself with his measurements. *Bob* constructs his table list of random basis and also obtains his candidate key, which will of course be different from *Eve*'s. When* Alice* broadcasts his basis table on the classical channel and *Bob* sift his  key accordingly, he will obtain a key different from *Alice*'s, unusable, since even in the same basis choice qubits will be different about **$1/4$** of the times. If *Alice* try to encrypt a message, symmetrical cryptography would fail and both *Alice* and *Bob* will know that communication has been compromised. \n",
    "\n",
    "If *Alice* and *Bob* never compare their measurement and they only compare basis tables they have no way of knowing that the state has been altered, until the encrypted message is produced, sent and decryption fails. However they can decide to initiate **key sharing** by also comparing their measurement on a certain number of qubits, and, only when they are convinced that the channel is free of interference, they proceed with the actual key sharing. Of course the part of the key that represents the unveiled measurement has to be discarded from it. In real world application is comprises about $1/3$ of the whole key.\n",
    "\n",
    "In this notebook I will be demonstrating exactly this behavior, how initial key sharing can be used to detect an eavesdropper. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## QKD proof of concept on a quantum  computer\n",
    "Quantum Key Distribution requires special apparatus made for key sharing. Having at our disposal IBM's quantum computer, here we present a proof-of-concept of how the process can be realized, using real quantum measuring devices. \n",
    "\n",
    "The key sharing part will be simulated using different quantum circuits one for each party (*Alice*, *Bob*, *Eve*) in the exchange, since we don't have a real quantum channel. We present first the simple case in which only *Alice* and *Bob* are present, and we later proceed to introduce *Eve* and demonstrate how she can be caught.\n",
    "\n",
    "First we check for and import the required libraries: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import numpy for random number generation\n",
    "import numpy as np\n",
    "\n",
    "# importing Qiskit\n",
    "from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, execute, BasicAer\n",
    "\n",
    "# Import basic plotting tools\n",
    "from qiskit.tools.visualization import plot_histogram"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next we do some preliminary settings to better manipulate quantum circuits and we set the number of available (qu)bits to 16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Creating registers with n qubits\n",
    "n = 16  # for a local backend n can go as up as 23, after that it raises a Memory Error\n",
    "qr = QuantumRegister(n, name='qr')\n",
    "cr = ClassicalRegister(n, name='cr')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We create Alice's quantum circuit, made of $n$ qubits (and $n$ bits in a classical register, for measuring). We use $randint$ from numpy to generate a random number in the available range which will be our key and then we write the resulted number in binary and we memorize the key in a proper variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Quantum circuit for alice state\n",
    "alice = QuantumCircuit(qr, cr, name='Alice')\n",
    "\n",
    "# Generate a random number in the range of available qubits [0,65536))\n",
    "alice_key = np.random.randint(0, high=2**n)\n",
    "\n",
    "# Cast key to binary for encoding\n",
    "# range: key[0]-key[15] with key[15] least significant figure\n",
    "alice_key = np.binary_repr(alice_key, n) # n is the width"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Parse the generated key and we encode it in Alice's circuit, initializing her qubits to the computational basis: $\\{\\lvert 0 \\rangle,\\  \\lvert 1 \\rangle\\}$, according to the value bit. Then we apply a rotation to about half of these qubits, so that about $1/2$ of them will now be in one of the eigenstates of the diagonal basis:  $\\{\\lvert \\nearrow \\rangle,\\  \\lvert \\searrow \\rangle\\}$. We record the basis choice in a list (table) that will later be used for key verification."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Encode key as alice qubits \n",
    "# IBM's qubits are all set to |0> initially\n",
    "for index, digit in enumerate(alice_key):\n",
    "    if digit == '1':\n",
    "        alice.x(qr[index]) # if key has a '1', change state to |1>\n",
    "        \n",
    "# Switch randomly about half qubits to diagonal basis\n",
    "alice_table = []        # Create empty basis table\n",
    "for index in range(len(qr)):       # BUG: enumerate(q) raises an out of range error\n",
    "    if 0.5 < np.random.random():   # With 50% chance...\n",
    "        alice.h(qr[index])         # ...change to diagonal basis\n",
    "        alice_table.append('X')    # character for diagonal basis\n",
    "    else:\n",
    "        alice_table.append('Z')    # character for computational basis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "How can we send this state to Bob? As said, we don't have another quantum computer, but we can create another quantum circuit, which we call $bob$, and initialize Bob's initial state to Alice's output state. To accomplish this task we define a helper function, *SendState*, that retrieves the qasm code of a given quantum circuit, $qc1$, does some filtering to extract the quantum gates applied, and produces new instructions that uses to initialize another circuit, $qc2$. This trick works because QISKit maintains a python dictionary of quantum circuits with their relative qasm instructions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get_qasm method needs the str label\n",
    "# alternatively we can use circuits[0] but since dicts are not ordered\n",
    "# it is not a good idea to put them in a func\n",
    "# circuits = list(qp.get_circuit_names())\n",
    "\n",
    "def SendState(qc1, qc2, qc1_name):\n",
    "    ''' This function takes the output of a circuit qc1 (made up only of x and \n",
    "        h gates and initializes another circuit qc2 with the same state\n",
    "    ''' \n",
    "    \n",
    "    # Quantum state is retrieved from qasm code of qc1\n",
    "    qs = qc1.qasm().split(sep=';')[4:-1]\n",
    "\n",
    "    # Process the code to get the instructions\n",
    "    for index, instruction in enumerate(qs):\n",
    "        qs[index] = instruction.lstrip()\n",
    "\n",
    "    # Parse the instructions and apply to new circuit\n",
    "    for instruction in qs:\n",
    "        if instruction[0] == 'x':\n",
    "            old_qr = int(instruction[5:-1])\n",
    "            qc2.x(qr[old_qr])\n",
    "        elif instruction[0] == 'h':\n",
    "            old_qr = int(instruction[5:-1])\n",
    "            qc2.h(qr[old_qr])\n",
    "        elif instruction[0] == 'm': # exclude measuring:\n",
    "            pass\n",
    "        else:\n",
    "            raise Exception('Unable to parse instruction')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can create Bob's circuit and \"send\" Alice's qubits to Bob. We pretend that this state is unknown to Bob so that he doesn't know which basis to use and decides randomly that $1/2$ of the qubits are to be measured in the rectilinear basis and the other $1/2$ in the diagonal basis; we then record Bob's choice in his table list variable "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "bob = QuantumCircuit(qr, cr, name='Bob')\n",
    "\n",
    "SendState(alice, bob, 'Alice')    \n",
    "\n",
    "# Bob doesn't know which basis to use\n",
    "bob_table = []\n",
    "for index in range(len(qr)): \n",
    "    if 0.5 < np.random.random():  # With 50% chance...\n",
    "        bob.h(qr[index])        # ...change to diagonal basis\n",
    "        bob_table.append('X')\n",
    "    else:\n",
    "        bob_table.append('Z')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Bob can now go ahead and measure all his qubits and store the measurement in the classical register. We build and run the circuit on the local backend, but, if a token is provided and enough credits are available, it can also be executed on the remote backend with 16 qubits, ibmqx5. Note that is very important that $shots=1$, since we have to pretend that Bob has only one measurement chance, otherwise he could statistically infer the basis used (you can try). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAdAAAAGyCAYAAABUYZVRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzt3Xm0XGWZ7/HvQyAQBAKJwQwaTQAxIsYQREGEMEQFh8ugDIqIE40Kto2g2FevYouuRsar0oqtIupFGpw1CoYgyDwFDYSOBjFpyQBkYIhAQvLcP/Y+WFROcqp2zjlV59T3s1atU3vvt3Y9tZbmx7v3+747MhNJktSczVpdgCRJA5EBKklSBQaoJEkVGKCSJFVggEqSVIEBKklSBQaoJEkVGKCSJFVggEqSVMHmrS6glUaOHJnjx49vdRmSpDZy9913P5KZo3pq19EBOn78eGbNmtXqMiRJbWTEiBELGmnnJVxJkiowQCVJqsAAlSSpAgNUkqQKDFBJkiowQCVJqsAAlSSpAgNUkqQKDFBJkiowQCVJqsAAlSSpAgNUkqQKDFBJkiowQCVJqsAAlSSpAgNUkqQKDFBJkiowQCVJqsAAlSSpAgNUkqQKDFBJkiowQCVJqsAAlSSpAgNUGoROPvlkXvrSl7LPPvt0ezwzOeOMM5g6dSr77rsvf/jDH549dtlll7Hnnnuy5557ctlllz27/+677+Z1r3sdU6dO5YwzziAz+/x3SO3MAJUG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JUkqQIDVJKkCgxQSZIq+P9cAXvAD1PJOAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Measure all qubits\n",
    "for index in range(len(qr)): \n",
    "    bob.measure(qr[index], cr[index])\n",
    "    \n",
    "# Execute the quantum circuit \n",
    "backend = BasicAer.get_backend('qasm_simulator')    \n",
    "result = execute(bob, backend=backend, shots=1).result()\n",
    "plot_histogram(result.get_counts(bob))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Result of the measure is Bob's key candidate\n",
    "bob_key = list(result.get_counts(bob))[0]\n",
    "bob_key = bob_key[::-1]      # key is reversed so that first qubit is the first element of the list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The histogram is not highly informative of course, but we can see that the measure has been performed correctly. Alice and Bob can switch over to the classical channel, compare their basis table lists, and discard qubits measured using different basis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Different choice for qubit: 0, Alice has Z, Bob has X\n",
      "Same choice for qubit: 1, basis: Z\n",
      "Different choice for qubit: 2, Alice has X, Bob has Z\n",
      "Same choice for qubit: 3, basis: Z\n",
      "Same choice for qubit: 4, basis: X\n",
      "Different choice for qubit: 5, Alice has X, Bob has Z\n",
      "Different choice for qubit: 6, Alice has Z, Bob has X\n",
      "Same choice for qubit: 7, basis: X\n",
      "Different choice for qubit: 8, Alice has Z, Bob has X\n",
      "Different choice for qubit: 9, Alice has X, Bob has Z\n",
      "Different choice for qubit: 10, Alice has X, Bob has Z\n",
      "Different choice for qubit: 11, Alice has Z, Bob has X\n",
      "Same choice for qubit: 12, basis: X\n",
      "Different choice for qubit: 13, Alice has X, Bob has Z\n",
      "Different choice for qubit: 14, Alice has Z, Bob has X\n",
      "Different choice for qubit: 15, Alice has X, Bob has Z\n"
     ]
    }
   ],
   "source": [
    "keep = []\n",
    "discard = []\n",
    "for qubit, basis in enumerate(zip(alice_table, bob_table)):\n",
    "    if basis[0] == basis[1]:\n",
    "        print(\"Same choice for qubit: {}, basis: {}\" .format(qubit, basis[0])) \n",
    "        keep.append(qubit)\n",
    "    else:\n",
    "        print(\"Different choice for qubit: {}, Alice has {}, Bob has {}\" .format(qubit, basis[0], basis[1]))\n",
    "        discard.append(qubit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We know that Bob will pick the wrong basis for $1/2$ of the qubits, so we can check that this theoretical probability is indeed replicated. We also know that although Bob picks the wrong basis, he can still end up with right eigenstate, and that he will do so about $1/2$ of the times, getting right $3/4$ of the qubits. We can check when Alice's and Bob's measurements coincide due to pure chance, although noting that this step is never performed in the actual key sharing step, but only in the inital sharing to test for eavesdropper."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Percentage of qubits to be discarded according to table comparison:  0.3125\n",
      "Measurement convergence by additional chance:  0.75\n"
     ]
    }
   ],
   "source": [
    "acc = 0\n",
    "for bit in zip(alice_key, bob_key):\n",
    "    if bit[0] == bit[1]:\n",
    "        acc += 1\n",
    "\n",
    "print('Percentage of qubits to be discarded according to table comparison: ', len(keep)/n)\n",
    "print('Measurement convergence by additional chance: ', acc/n)                "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now before sifting the keys we perform a check on a certain number of the qubits, comparing their value to see if they have been altered. Since we have only 16 qubits, which is a really low number, we check all of them. Although the procedure is limited to exchange 16 qubits at a time it can be repeated as many times as needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Percentage of similarity between the keys:  1.0\n"
     ]
    }
   ],
   "source": [
    "new_alice_key = [alice_key[qubit] for qubit in keep]\n",
    "new_bob_key = [bob_key[qubit] for qubit in keep]\n",
    "\n",
    "acc = 0\n",
    "for bit in zip(new_alice_key, new_bob_key):\n",
    "    if bit[0] == bit[1]:\n",
    "        acc += 1        \n",
    "        \n",
    "print('Percentage of similarity between the keys: ', acc/len(new_alice_key))              "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If the qubits measured are the same can accept the new sifted keys. The new sifted keys are printed to stdout, of course this step is just to verify the rightness of the protocol, when the procedure is repeated, each party is not supposed to know the other's sifted key. \n",
    "\n",
    "Note that, in the real world, quantum channel are subject to information loss since detectors are not perfectly efficient and some photons are going to be lost along the way. Thus, the similarity between the keys will hardly be $1.0$, but surely not as low as $0.75$ which we know is the case in which it has been eavesdropped. As a percentage cut-off we can pick $0.9$ and perform a check before calling the exchange successfull or invalid. You can try to insert a parameter that represents this loss as exercise. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Key exchange has been successfull\n",
      "New Alice's key:  ['1', '1', '1', '0', '1']\n",
      "New Bob's key:  ['1', '1', '1', '0', '1']\n"
     ]
    }
   ],
   "source": [
    "if (acc//len(new_alice_key) == 1):\n",
    "    print(\"Key exchange has been successfull\")\n",
    "    print(\"New Alice's key: \", new_alice_key)\n",
    "    print(\"New Bob's key: \", new_bob_key)\n",
    "else:\n",
    "    print(\"Key exchange has been tampered! Check for eavesdropper or try again\")\n",
    "    print(\"New Alice's key is invalid: \", new_alice_key)\n",
    "    print(\"New Bob's key is invalid: \", new_bob_key)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Everything overlaps perfectly, that is indeed almost trivial. It's time to introduce Eve, the eavesdropper, and see what happens. We create Eve's circuit and we initiliaze it to Alice's state. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "eve = QuantumCircuit(qr, cr, name='Eve')\n",
    "SendState(alice, eve, 'Alice')   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Just like Bob, Eve doesn't know which basis to use and she picks them randomly while recording her choice in a (table) list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "eve_table = []\n",
    "for index in range(len(qr)): \n",
    "    if 0.5 < np.random.random(): \n",
    "        eve.h(qr[index])        \n",
    "        eve_table.append('X')\n",
    "    else:\n",
    "        eve_table.append('Z')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "She measures according to her basis choice and she generates her candidate key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "for index in range(len(qr)): \n",
    "    eve.measure(qr[index], cr[index])\n",
    "    \n",
    "# Execute (build and run) the quantum circuit \n",
    "backend = BasicAer.get_backend('qasm_simulator')    \n",
    "result = execute(eve, backend=backend, shots=1).result()\n",
    "\n",
    "# Result of the measure is Eve's key\n",
    "eve_key = list(result.get_counts(eve))[0]\n",
    "eve_key = eve_key[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Up to now, Eve did exactly what Bob did in the previous example. From this point on, however, things are a bit tricky. Eve has measured the state causing qubits to collapse in different eigenstates. This property is not easy to implement in QISKit because measurement results are stored in classical registered, while the qubits themselves are \"unchanged\". Therefore we need to update Eve's qubits to the new altered states starting from the results of the measures (Eve's key), reversing the instructions that Eve has executed, and apply them to qubits when necessary, which means when the basis choice was different.\n",
    "\n",
    "You can try figure out yourself how a state is changed after a measurement, but remember that unitary operators in general don't commute."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Same choice for qubit: 0, basis: Z\n",
      "Same choice for qubit: 1, basis: Z\n",
      "Same choice for qubit: 2, basis: X\n",
      "Different choice for qubit: 3, Alice has Z, Eve has X\n",
      "Same choice for qubit: 4, basis: X\n",
      "Different choice for qubit: 5, Alice has X, Eve has Z\n",
      "Different choice for qubit: 6, Alice has Z, Eve has X\n",
      "Different choice for qubit: 7, Alice has X, Eve has Z\n",
      "Different choice for qubit: 8, Alice has Z, Eve has X\n",
      "Same choice for qubit: 9, basis: X\n",
      "Different choice for qubit: 10, Alice has X, Eve has Z\n",
      "Same choice for qubit: 11, basis: Z\n",
      "Same choice for qubit: 12, basis: X\n",
      "Different choice for qubit: 13, Alice has X, Eve has Z\n",
      "Different choice for qubit: 14, Alice has Z, Eve has X\n",
      "Same choice for qubit: 15, basis: X\n"
     ]
    }
   ],
   "source": [
    "# Update states to new eigenstates (of wrongly chosen basis)\n",
    "for qubit, basis in enumerate(zip(alice_table, eve_table)):\n",
    "    if basis[0] == basis[1]:\n",
    "        print(\"Same choice for qubit: {}, basis: {}\" .format(qubit, basis[0]))\n",
    "    else:\n",
    "        print(\"Different choice for qubit: {}, Alice has {}, Eve has {}\" .format(qubit, basis[0], basis[1]))\n",
    "        if eve_key[qubit] == alice_key[qubit]:\n",
    "            eve.h(qr[qubit])\n",
    "        else:\n",
    "            if basis[0] == 'X' and basis[1] == 'Z':\n",
    "                eve.h(qr[qubit])\n",
    "                eve.x(qr[qubit])\n",
    "            else:\n",
    "                eve.x(qr[qubit])\n",
    "                eve.h(qr[qubit])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Eve's altered state is now sent to Bob that performs the usual routine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAdAAAAGxCAYAAADS9ef/AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzt3Xu8VXWd//HXx6MopKIgBoeRAk1DMwRMR7yEFyqtKbPJtDGtxhyt7GJaOj+7OJP5G0ftfhmt+anZqGnZFSdFSM1bIlgoRWEGKV4BzfAC4uf3x1pHt4cD5+zlOWfvc/br+Xjsx9nru7577c/+Q96utb7r+43MRJIk1WejRhcgSdJAZIBKklSBASpJUgUGqCRJFRigkiRVYIBKklSBASpJUgUGqCRJFRigkiRVsHGjC2ikkSNH5rhx4xpdhiSpidx5552PZuao7vq1dICOGzeO2bNnN7oMSVITGTFixJKe9PMSriRJFRigkiRVYIBKklSBASpJUgUGqCRJFRigkiRVYIBKklSBASpJUgUGqCRJFRigkiRVYIBKklSBASpJUgUGqCRJFRigkiRVYIBKklSBASpJUgUGqCRJFRigkiRVYIBKklSBASpJUgUGqCRJFRigkiRVYIBKklSBASoNQh/+8IfZcccdmTZtWpf7M5NTTz2VqVOnss8++/Cb3/zm+X2XXnopu+++O7vvvjuXXnrp8+133nkne++9N1OnTuXUU08lM/v8d0jNzACVBqF3v/vdXHHFFevdP2vWLO655x7mzp3LF7/4RT7xiU8AsHLlSs4++2yuvfZaZs2axdlnn81jjz0GwMknn8wXv/hF5s6dyz333MOsWbP65bdIzcoAlQahadOmsfXWW693/8yZMzniiCOICF73utfx17/+lQcffJDZs2czffp0tt56a7baaiumT5/Oddddx4MPPsgTTzzBHnvsQURwxBFHMHPmzH78RVLzMUClFvTAAw8wduzY57fb29t54IEHWLZs2Trty5Yt44EHHqC9vX2d/lIrM0ClFtTV/cuIqLtdamUGqNSC2tvbuf/++5/fXrZsGaNHj2bs2LHrtI8ZM+b5M9HO/aVWZoBKLejggw/msssuIzO5/fbb2XLLLRk9ejQHHHAAc+bM4bHHHuOxxx5jzpw5HHDAAYwePZrNN9+c22+/nczksssu45BDDmn0z5AaauNGFyCp9x177LHcdNNNLF++nF122YVTTz2VZ599FoD3ve99zJgxg2uvvZapU6cydOhQvva1rwGw9dZbc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cwywF3ARxVnoMcBXI2Lnsm9bo+qUWpFnoFITi4gTgaMyc88u9u1D8UjLWGCPzHy0v+uTWplnoFJzWw1sERGvgWIFlo5ZhjLzVxSLZj8NvKFxJUqtyQCVmtuVFJdrPxYRW2TmM5m5umNUbmYuBR4D/q6RRUqtyACVmlQ5snYFcDowA1gWEd/pmFA+IsZFxFHArsD3G1ep1Jq8Byo1uYjYChhH8Zzn24G9y10PUvxP8MWZ+bnGVCe1LgNUakIRsS3wHuATFPPfPkVxqfZG4DZgE2B74BfAH9P/kKV+Z4BKTSgiLgR2AX5KcRl3BMWl2h2Bh4HTM/O2hhUoyQCVmk157/MJ4JDMvKGmbRzFMmb/DEwADs/MeQ0rVGpxDiKSms/OwL0Uj7AAkIUlmXk58A8Ul3Pf2aD6JGGASs3oTxSXab8YEa/qYiL5ZyhmIzq4EcVJKhigUpPJzKeA/wMMBS4Gjo6I7SLiZQARMQx4PXBX46qU5D1QqUmVsw99GngrxSTytwCPAAcBDwDHZuaCxlUotTYDVGpy5SMtbwYOpZi27y7gisz8fUMLk1qcASoNIBGxUWY+1+g6JBmgkiRV4iAiSZIqMEAlSarAAJUkqQIDVJKkCgxQSZIqMEAlSarAAJUkqYL/DwW96KgHjFDoAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SendState(eve, bob, 'Eve')\n",
    "          \n",
    "bob_table = []\n",
    "for index in range(len(qr)): \n",
    "    if 0.5 < np.random.random(): \n",
    "        bob.h(qr[index])      \n",
    "        bob_table.append('X')\n",
    "    else:\n",
    "        bob_table.append('Z')\n",
    "          \n",
    "for index in range(len(qr)): \n",
    "    bob.measure(qr[index], cr[index])\n",
    "          \n",
    "result = execute(bob, backend=backend, shots=1).result()\n",
    "plot_histogram(result.get_counts(bob))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "bob_key = list(result.get_counts(bob))[0]\n",
    "bob_key = bob_key[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After the measure Alice and Bob share the basis table lists and perform the usual checks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Same choice for qubit: 0, basis: Z\n",
      "Different choice for qubit: 1, Alice has Z, Bob has X\n",
      "Same choice for qubit: 2, basis: X\n",
      "Different choice for qubit: 3, Alice has Z, Bob has X\n",
      "Different choice for qubit: 4, Alice has X, Bob has Z\n",
      "Same choice for qubit: 5, basis: X\n",
      "Different choice for qubit: 6, Alice has Z, Bob has X\n",
      "Same choice for qubit: 7, basis: X\n",
      "Different choice for qubit: 8, Alice has Z, Bob has X\n",
      "Different choice for qubit: 9, Alice has X, Bob has Z\n",
      "Different choice for qubit: 10, Alice has X, Bob has Z\n",
      "Same choice for qubit: 11, basis: Z\n",
      "Same choice for qubit: 12, basis: X\n",
      "Same choice for qubit: 13, basis: X\n",
      "Different choice for qubit: 14, Alice has Z, Bob has X\n",
      "Same choice for qubit: 15, basis: X\n",
      "\n",
      "Percentage of qubits to be discarded according to table comparison:  0.5\n",
      "Measurement convergence by additional chance:  0.4375\n",
      "\n",
      "Percentage of similarity between the keys:  0.5\n",
      "\n",
      "Key exchange has been tampered! Check for eavesdropper or try again\n",
      "New Alice's key is invalid:  ['0', '0', '1', '0', '1', '1', '0', '0']\n",
      "New Bob's key is invalid:  ['0', '0', '0', '0', '0', '0', '1', '0']\n"
     ]
    }
   ],
   "source": [
    "keep = []\n",
    "discard = []\n",
    "for qubit, basis in enumerate(zip(alice_table, bob_table)):\n",
    "    if basis[0] == basis[1]:\n",
    "        print(\"Same choice for qubit: {}, basis: {}\" .format(qubit, basis[0])) \n",
    "        keep.append(qubit)\n",
    "    else:\n",
    "        print(\"Different choice for qubit: {}, Alice has {}, Bob has {}\" .format(qubit, basis[0], basis[1]))\n",
    "        discard.append(qubit)\n",
    "        \n",
    "acc = 0\n",
    "for bit in zip(alice_key, bob_key):\n",
    "    if bit[0] == bit[1]:\n",
    "        acc += 1\n",
    "\n",
    "print('\\nPercentage of qubits to be discarded according to table comparison: ', len(keep)/n)\n",
    "print('Measurement convergence by additional chance: ', acc/n)  \n",
    "\n",
    "new_alice_key = [alice_key[qubit] for qubit in keep]\n",
    "new_bob_key = [bob_key[qubit] for qubit in keep]\n",
    "\n",
    "acc = 0\n",
    "for bit in zip(new_alice_key, new_bob_key):\n",
    "    if bit[0] == bit[1]:\n",
    "        acc += 1        \n",
    "        \n",
    "print('\\nPercentage of similarity between the keys: ', acc/len(new_alice_key)) \n",
    "\n",
    "if (acc//len(new_alice_key) == 1):\n",
    "    print(\"\\nKey exchange has been successfull\")\n",
    "    print(\"New Alice's key: \", new_alice_key)\n",
    "    print(\"New Bob's key: \", new_bob_key)\n",
    "else:\n",
    "    print(\"\\nKey exchange has been tampered! Check for eavesdropper or try again\")\n",
    "    print(\"New Alice's key is invalid: \", new_alice_key)\n",
    "    print(\"New Bob's key is invalid: \", new_bob_key)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see when Alice and Bob reveal their key to each other they notice a discordance: Eve has been caught!  To really get the percentages right, you can try repeating the experiment multiple times or you can write a higher routine that iterates the key sharing; in either case you will se that they converge to the expected values."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
